package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.crm.entity.GwgcT;
import cn.e4j.modular.crm.entity.GwgdT;
import cn.e4j.modular.crm.entity.GwgeT;
import cn.e4j.modular.crm.mapper.GwgcTMapper;
import cn.e4j.modular.crm.service.GwgcTService;
import cn.e4j.modular.oa.service.OAAbandonWsService;
import cn.e4j.modular.oa.ws.common.SoapService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuan_rz
 * @since 2019-11-20
 */
@Service
public class GwgcTServiceImpl extends ServiceImpl<GwgcTMapper, GwgcT> implements GwgcTService {

    @Autowired
    OAAbandonWsService oaAbandonWsService;

    @Override
    @Transactional(readOnly = true)
    public Dto getAddData(Dto dto){
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        outDto.put("GWGCORIG", shiroUser.getORG_ID());
        outDto.put("GWGCORIU", shiroUser.getID());
        outDto.put("GWGCORIU_NAME", shiroUser.getNAME());
        outDto.put("GWGCDATE", DateUtils.formatDate(new Date(), "yyyy-MM-dd"));
        Dto orgData = this.baseMapper.getOrgName(outDto);
        outDto.put("GWGCORIG_NAME", orgData != null ? orgData.getString("NAME"):"");
        return outDto;
    }

    @Override
    @Transactional
    public void addData(List<GwgdT> gwgdTList, GwgcT gwgcT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        String docno = this.getGwgcId();
        //投标车型明细新增
        for(GwgdT gwgdT : gwgdTList){
            gwgdT.setGWGDENT(shiroUser.getDBC());
            gwgdT.setGWGDID(docno);
            gwgdT.setGWGDUSER(shiroUser.getID());
            gwgdT.setGWGDGRUP(shiroUser.getORG_ID());
            gwgdT.setGWGDTIME(DateUtils.formatDate(new Date(),"HH:mm:ss"));
            gwgdT.setGWGDORIG(shiroUser.getORG_ID());
            gwgdT.setGWGDORIU(shiroUser.getID());
            gwgdT.setGWGDDATE(LocalDate.now());
            gwgdT.insert();
        }

        gwgcT.setGWGCENT(shiroUser.getDBC());
        gwgcT.setGWGCID(docno);
        gwgcT.setGWGCUSER(shiroUser.getORG_ID());
        gwgcT.setGWGCGRUP(shiroUser.getID());
        gwgcT.setGWGCTIME(DateUtils.formatDate(new Date(),"HH:mm:ss"));
        gwgcT.insert();
    }

    @Override
    @Transactional
    public void editData(List<GwgdT> gwgdTList, GwgcT gwgcT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwgdT gwgdTDel = new GwgdT();
        UpdateWrapper<GwgdT> gwgdTUpdateWrapper = new UpdateWrapper<>();
        gwgdTUpdateWrapper.eq("GWGDENT", shiroUser.getDBC());
        gwgdTUpdateWrapper.eq("GWGDID", gwgcT.getGWGCID());
        gwgdTDel.delete(gwgdTUpdateWrapper);

        //投标车型明细新增
        for(GwgdT gwgdT : gwgdTList){
            gwgdT.setGWGDENT(shiroUser.getDBC());
            gwgdT.setGWGDID(gwgcT.getGWGCID());
            gwgdT.setGWGDUSER(shiroUser.getID());
            gwgdT.setGWGDGRUP(shiroUser.getORG_ID());
            gwgdT.setGWGDTIME(DateUtils.formatDate(new Date(),"HH:mm:ss"));
            gwgdT.setGWGDORIG(shiroUser.getORG_ID());
            gwgdT.setGWGDORIU(shiroUser.getID());
            gwgdT.setGWGDDATE(LocalDate.now());
            gwgdT.insert();
        }

        gwgcT.setGWGCMODU(shiroUser.getID());
        UpdateWrapper<GwgcT> gwgcTUpdateWrapper = new UpdateWrapper<>();
        gwgcTUpdateWrapper.eq("GWGCENT", shiroUser.getDBC());
        gwgcTUpdateWrapper.eq("GWGCID", gwgcT.getGWGCID());
        gwgcT.update(gwgcTUpdateWrapper);
    }

    @Override
    @Transactional
    public void deleteData(GwgcT gwgcT) {
        GwgdT gwgdT = new GwgdT();
        UpdateWrapper<GwgdT> gwgdTUpdateWrapper = new UpdateWrapper<>();
        gwgdTUpdateWrapper.eq("GWGDENT", gwgcT.getGWGCENT());
        gwgdTUpdateWrapper.eq("GWGDID", gwgcT.getGWGCID());

        GwgeT gwgeT = new GwgeT();
        UpdateWrapper<GwgeT> gwgeTUpdateWrapper = new UpdateWrapper<>();
        gwgeTUpdateWrapper.eq("GWGEENT", gwgcT.getGWGCENT());
        gwgeTUpdateWrapper.eq("GWGEID", gwgcT.getGWGCID());

        UpdateWrapper<GwgcT> gwgcTUpdateWrapper = new UpdateWrapper<>();
        gwgcTUpdateWrapper.eq("GWGCENT", gwgcT.getGWGCENT());
        gwgcTUpdateWrapper.eq("GWGCID", gwgcT.getGWGCID());

        gwgdT.delete(gwgdTUpdateWrapper);
        gwgeT.delete(gwgeTUpdateWrapper);
        gwgcT.delete(gwgcTUpdateWrapper);
    }

    @Override
    @Transactional
    public void delDataBatch(List<GwgcT> gwgcTList) {
        for(GwgcT gwgcT : gwgcTList) {
            GwgdT gwgdT = new GwgdT();
            UpdateWrapper<GwgdT> gwgdTUpdateWrapper = new UpdateWrapper<>();
            gwgdTUpdateWrapper.eq("GWGDENT", gwgcT.getGWGCENT());
            gwgdTUpdateWrapper.eq("GWGDID", gwgcT.getGWGCID());

            GwgeT gwgeT = new GwgeT();
            UpdateWrapper<GwgeT> gwgeTUpdateWrapper = new UpdateWrapper<>();
            gwgeTUpdateWrapper.eq("GWGEENT", gwgcT.getGWGCENT());
            gwgeTUpdateWrapper.eq("GWGEID", gwgcT.getGWGCID());

            UpdateWrapper<GwgcT> gwgcTUpdateWrapper = new UpdateWrapper<>();
            gwgcTUpdateWrapper.eq("GWGCENT", gwgcT.getGWGCENT());
            gwgcTUpdateWrapper.eq("GWGCID", gwgcT.getGWGCID());

            gwgdT.delete(gwgdTUpdateWrapper);
            gwgeT.delete(gwgeTUpdateWrapper);
            gwgcT.delete(gwgcTUpdateWrapper);
        }
    }

    @Override
    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwgdData(Dto dto) {
        List<Map<String, Object>> gwgdDtos = this.baseMapper.getGwgdData(dto);
        return gwgdDtos;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwgeList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwgeDtos = this.baseMapper.getGwgeData(page, dto);
        return gwgeDtos;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwgeData(Dto dto) {
        List<Map<String, Object>> gwgeDtos = this.baseMapper.getGwgeData(dto);
        return gwgeDtos;
    }

    @Override
    @Transactional
    public void addGwgeData(List<GwgdT> gwgdTList, List<GwgeT> gwgeTList, GwgcT gwgcT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        //删除中标信息
        GwgeT gwgeTDel = new GwgeT();
        UpdateWrapper<GwgeT> gwgeTUpdateWrapper = new UpdateWrapper<>();
        gwgeTUpdateWrapper.eq("GWGEENT", gwgcT.getGWGCENT());
        gwgeTUpdateWrapper.eq("GWGEID", gwgcT.getGWGCID());
        gwgeTDel.delete(gwgeTUpdateWrapper);

        //投标车型明细修改
        for(GwgdT gwgdT : gwgdTList){
            UpdateWrapper<GwgdT> gwgdTUpdateWrapper = new UpdateWrapper<>();
            gwgdTUpdateWrapper.eq("GWGDENT", gwgcT.getGWGCENT());
            gwgdTUpdateWrapper.eq("GWGDID", gwgcT.getGWGCID());
            gwgdTUpdateWrapper.eq("GWGD002", gwgdT.getGWGD002());
            gwgdT.update(gwgdTUpdateWrapper);
        }

        //投标车型中标详情修改
        for(GwgeT gwgeT : gwgeTList){
            gwgeT.setGWGEENT(gwgcT.getGWGCENT());
            gwgeT.setGWGEID(gwgcT.getGWGCID());
            gwgeT.setGWGEUSER(shiroUser.getID());
            gwgeT.setGWGEGRUP(shiroUser.getORG_ID());
            gwgeT.setGWGEORIG(shiroUser.getORG_ID());
            gwgeT.setGWGEORIU(shiroUser.getID());
            gwgeT.setGWGEDATE(LocalDate.now());
            gwgeT.setGWGETIME(DateUtils.formatDate(new Date(),"HH:mm:ss"));
            gwgeT.insert();
        }

        gwgcT.setGWGCMODU(shiroUser.getID());
        UpdateWrapper<GwgcT> gwgcTUpdateWrapper = new UpdateWrapper<>();
        gwgcTUpdateWrapper.eq("GWGCENT", gwgcT.getGWGCENT());
        gwgcTUpdateWrapper.eq("GWGCID", gwgcT.getGWGCID());
        gwgcT.update(gwgcTUpdateWrapper);
    }

    @Transactional
    @Override
    public Dto updateStatus(List<GwgcT> gwgcTList) {
        String state = gwgcTList.get(0).getGWGCACTI();
        Dto outDto = Dtos.newDto();
        for (GwgcT gwgcT : gwgcTList) {
            UpdateWrapper<GwgcT> gwgcTUpdateWrapper = new UpdateWrapper<>();
            gwgcTUpdateWrapper.eq("GWGCENT", gwgcT.getGWGCENT());
            gwgcTUpdateWrapper.eq("GWGCID", gwgcT.getGWGCID());
            gwgcT.update(gwgcTUpdateWrapper);
        }
        outDto.setAppCode(200);
        return outDto;
    }

    @Override
    @Transactional(readOnly = true)
    public Dto getGwgeCount(Dto dto){
        Dto outDto = this.baseMapper.getGwgeCount(dto);
        return outDto;
    }

    @Override
    @Transactional(readOnly = true)
    public String getGwgcId(){
        String id = InvokeUtil.invokeBeanMethod2("cn.e4j.modular.api.mapper.GwgcTMapper.getMaxGwgcId",DateUtils.getDate("yyMMdd"));
        if(id == null) {
            id = DateUtils.getDate("yyMMdd") + "0001";
        }
        return id;
    }

    @Override
    public Boolean existedTheParams(GwgcT gwgcT){
        QueryWrapper<GwgcT> gwgcTQueryWrapper = new QueryWrapper<>();
        gwgcTQueryWrapper.eq("GWGCENT", ShiroKit.getUser().getDBC());
        gwgcTQueryWrapper.eq("GWGC001", gwgcT.getGWGC001());
        return this.baseMapper.selectList(gwgcTQueryWrapper).size() > 0;
    }

    private Dto oaAbandon(GwgcT gwgcT) {
        Dto outDto = Dtos.newDto();
        Dto paramDto = Dtos.newDto();
        paramDto.put("ID",gwgcT.getGWGC021());
        try{
            String resXml = oaAbandonWsService.callSoapWs(paramDto);
            JSONObject jsonObject = (JSONObject) SoapService.parseSoapRes(resXml,true).get("json");
            if(jsonObject.getString("flag").equals("1")){
                outDto.setAppCode(200);
            }else {
                outDto.setAppCode(500);
                outDto.setAppMsg(jsonObject.getString("msg"));
            }
        } catch (TimeoutException e){
            outDto.setAppCode(500);
            outDto.setAppMsg("连接超时，请检查网络配置");
            return outDto;
        } catch (Exception e) {
            outDto.setAppCode(500);
            outDto.setAppMsg("OA撤单操作失败");
            return outDto;
        }
        return outDto;
    }
}
